import pandas as pd
from DatetimeUtils import choose_day
from ListUtils import anti_join
import time
"""
上市公司财务财务指标数据
"""

def deal_append(query_start_, query_end_, engine, pro, ts_code):
    """
    通过anti_join函数处理增量数据
    """
    source_list_ = pro.fina_indicator(ts_code=ts_code, start_date=query_start_, end_date=query_end_)
    source_list_.drop_duplicates(subset=['ts_code', 'ann_date', 'end_date'], keep='first', inplace=True)
    # print('source_list_：(%s)' % source_list_)
    sql_ = 'select b.ts_code, b.ann_date, b.end_date from fina_indicator b WHERE b.ts_code = "' + ts_code + '" and b.ann_date >= ' + query_start_ + ' and b.ann_date <= ' + query_end_
    target_list_ = pd.read_sql(sql_, engine)
    # print('fina_indicator：(%s)' % target_list_)
    if len(target_list_) == 0:
        source_list_.to_sql('fina_indicator', con=engine, index=False, index_label=['ts_code', 'ann_date', 'end_date'], if_exists='append')
        print('source_list_：(%s)' % source_list_)
    else:
        # 数据库内有历史数据则增量添加
        source_list_ = anti_join(source_list_, target_list_, ['ts_code', 'ann_date', 'end_date'])
        print('source_list_：(%s)' % source_list_)
        if len(source_list_) > 0:
            source_list_.to_sql('fina_indicator', con=engine, index=False, index_label=['ts_code', 'ann_date', 'end_date'], if_exists='append')

    time.sleep(1)


def insert_history_data_fina_indicator(engine, pro):
    """
    是否查询并保存历史数据
    """
    insert_history_data_flag = True
    if insert_history_data_flag:

        # 股票列表
        sql = 'select s.ts_code from stock s'
        stock_list = pd.read_sql(sql, engine)

        if len(stock_list) > 0:
            for i in range(0, len(stock_list)):
                ts_code = stock_list.loc[i].ts_code
                current_day = choose_day(0).strftime('%Y%m%d')
                first_day = '19910101'
                deal_append(first_day, current_day, engine, pro, ts_code)


def update_current_data_fina_indicator(engine, pro):
    """
    更新当前数据，后续使用定时器
    """
    insert_current_flag = True
    if insert_current_flag:
        # 股票列表
        sql = 'select s.ts_code from stock s'
        stock_list = pd.read_sql(sql, engine)
        if len(stock_list) > 0:
            for i in range(0, len(stock_list)):
                ts_code = stock_list.loc[i].ts_code
                day_updates = 180  # 设定更新180天数据
                current_day = choose_day(0).strftime('%Y%m%d')
                that_day = choose_day(day_updates).strftime('%Y%m%d')
                deal_append(that_day, current_day, engine, pro, ts_code)